home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / xlib41 / examp3b.asm < prev    next >
Assembly Source File  |  1994-02-18  |  5KB  |  107 lines

  1. ;The following library should be combined with XLIBB.LIB using the Borland
  2. ;TLIB utility.
  3.  
  4.                MASM51                        ;Enforce MASM 5.1 compatibility
  5.                QUIRKS                        ;Allow MASM 5.1 quirks
  6.  
  7. PUSHW          MACRO IMMEDIATE16:REST        ;Macro to PUSH 16-bit constant
  8.                IF (@WordSize EQ 4)
  9.                DB             66H
  10.                ENDIF
  11.                DB             68H
  12.                DW             IMMEDIATE16
  13.                ENDM
  14.  
  15. PUSHD          MACRO IMMEDIATE32:REST        ;Macro to PUSH 32-bit constant
  16.                IF (@WordSize EQ 2)
  17.                DB             66H
  18.                ENDIF
  19.                DB             68H
  20.                DD             IMMEDIATE32
  21.                ENDM
  22.  
  23.                .MODEL         LARGE,PASCAL
  24.                .386P
  25.                .387
  26.  
  27.                INCLUDE        XLIBB.INC
  28.  
  29. CSEG           SEGMENT PARA PUBLIC USE16 'CODE'
  30.                ASSUME CS:CSEG, DS:DSEG
  31.  
  32. ;Function to calculate linear address from segment address on stack.  Returns
  33. ;linear address in DX:AX.
  34. LINADR         PROC FAR, SEGADR:DWORD
  35.                XOR            EAX,EAX        ;Clear high words
  36.                XOR            EDX,EDX
  37.                MOV            AX,WORD PTR SEGADR[0]
  38.                MOV            DX,WORD PTR SEGADR[2]
  39.                SHL            EDX,4          ;Calculate linear address
  40.                ADD            EDX,EAX
  41.                MOV            AX,DX
  42.                SHR            EDX,16         ;Return linear address in DX:AX
  43.                RET
  44. LINADR         ENDP
  45.  
  46. ;Structure defining control block for SUMARRAY.
  47. ARRAYDATA      STRUC
  48.   CONDCODE     DD             0              ;Condition code
  49.   N            DD             0              ;Number of elements to sum
  50.   ADDRESS      DD             0              ;Address of first element
  51.   SUM          DD             0              ;Sum of array elements
  52. ARRAYDATA      ENDS
  53.  
  54. ;Real-mode interface to SUMARRAY32.  Segment address of control block having
  55. ;structure ARRAYDATA should be on the stack.
  56. SUMARRAY       PROC FAR, CBSEGADR:DWORD
  57.                PUSH           DS
  58.                PUSHW          DSEG
  59.                POP            DS
  60.                XOR            EAX,EAX        ;Clear high words
  61.                XOR            EDX,EDX
  62.                MOV            AX,WORD PTR CBSEGADR[2]
  63.                MOV            DX,WORD PTR CBSEGADR[0]
  64.                SHL            EAX,4          ;Calculate linear address
  65.                ADD            EAX,EDX
  66.                MOV            CCODEPTR,EAX   ;Reset condition code address
  67.                POP            DS             ;Pop calling DS
  68.                PUSHD          OFFSET SUMARRAY32
  69.                CALL           ENTERPM        ;Execute SUMARRAY32 in protected
  70.                RET
  71. SUMARRAY       ENDP
  72.  
  73. CSEG           ENDS
  74.  
  75. TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  76.                ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  77.  
  78.                LARGESTACK
  79.  
  80. ;Sum the elements of a single precision array.  Array parameters are stored
  81. ;in a control block having structure of ARRAYDATA.  The linear address of the
  82. ;control block is stored at CCODEPTR.  An error code of -1 is returned in the
  83. ;condition code of the control block if the number of array elements is zero
  84. ;XLIB places an error code in the control block if an FPU exception occurs
  85. ;while calculating the sum.  This error code will have the FPU status word in
  86. ;the high word and the XLIB FPU error code in the low word.  Observe that this
  87. ;routine will be called with DS = FLATDSEL (flat-model data descriptor) and
  88. ;FS = DSEGSEL (DSEG data descriptor).
  89. SUMARRAY32     PROC NEAR
  90.                MOV            EBX,FS:CCODEPTR               ;Get control block
  91.                MOV            EDX,[EBX].ADDRESS             ;Get array address
  92.                MOV            ESI,[EBX].N                   ;Get N
  93.                SUB            ESI,1
  94.                JB             NODATA                        ;Error:  N = 0
  95.                FLDZ                                         ;Initialize sum
  96. SUMLOOP:       FADD           DWORD PTR [EDX+4*ESI]
  97.                SUB            ESI,1
  98.                JAE            SUMLOOP
  99.                FSTP           [EBX].SUM                     ;Save sum
  100.                RET
  101. NODATA:        MOV            [EBX].CONDCODE,-1             ;Record error code
  102.                RET
  103. SUMARRAY32     ENDP
  104.  
  105. TSEG           ENDS
  106.                END
  107.